﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
//
using System.Windows.Interop;
using System.Windows.Media;
//
// Mp3Cleaner, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//
namespace Mp3Cleaner
{
    class HeaderDropInfo
    {
        public string Name { get; set; }
    }

    public partial class MainWindow : Window
    {
        //
        private void dataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DependencyObject dep = (DependencyObject)e.OriginalSource;

            // iteratively traverse the visual tree
            while ((dep != null) && !(dep is DataGridCell) && !(dep is System.Windows.Controls.Primitives.DataGridColumnHeader))
            {
                if (!(dep is DependencyObject)) { dep = null; break; }
                if (dep is System.Windows.Documents.Run) { dep = null; break; } 
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null) return;

            if (dep is System.Windows.Controls.Primitives.DataGridColumnHeader)
            {
                System.Windows.Controls.Primitives.DataGridColumnHeader columnHeader = dep as System.Windows.Controls.Primitives.DataGridColumnHeader;
                //int n = columnHeader.DisplayIndex;
                string name = columnHeader.Column.SortMemberPath;
                //
                if ("tagTitle" == name || "FileName" == name || "tagTrack" == name || "FileTrack" == name)
                {
                    DragDrop.DoDragDrop(columnHeader, new HeaderDropInfo() { Name = name }, DragDropEffects.Copy);
                }
                return;
            }

            if (dep is DataGridCell)
            {
                DataGridCell cell = dep as DataGridCell;
                TextBlock tb = cell.Content as TextBlock;
                if (null == tb) return;//edycja

                string sss = ((TextBlock)cell.Content).Text;
                //
                DragDrop.DoDragDrop(tb, sss, DragDropEffects.Copy);
                return;
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------
        private void dataGrid1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void dataGrid1_DragLeave(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.None;
        }

        private void dataGrid1_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        private void dataGrid1_Drop(object sender, DragEventArgs e)
        {
            //---------------------------------------------------------------------------------------------------------------------------------

            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effects = DragDropEffects.Copy;
                string d = ((string)e.Data.GetData(DataFormats.UnicodeText));
                if (null == d) return;
                if ("" == d) return;
                //
                //
                DependencyObject dep = (DependencyObject)e.OriginalSource;

                // iteratively traverse the visual tree
                while ((dep != null) && !(dep is DataGridCell) && !(dep is System.Windows.Controls.Primitives.DataGridColumnHeader))
                {
                    if (!(dep is DependencyObject)) { dep = null; break; }
                    if (dep is System.Windows.Documents.Run) { dep = null; break; } 
                    dep = VisualTreeHelper.GetParent(dep);
                }

                if (dep == null) return;
                //
                #region HeaderDrop
                ///
                // Cell --> Header
                // Wypełnienie całej kolumny, nie wszystkie dozwolone
                ///
                if (dep is System.Windows.Controls.Primitives.DataGridColumnHeader)
                {
                    // Source - ColumnHeader
                    // Target
                    string columnName = "";
                    //DataGridColumn dgc = (dep as System.Windows.Controls.Primitives.DataGridColumnHeader).Column;
                    System.Windows.Controls.Primitives.DataGridColumnHeader dgch = dep as System.Windows.Controls.Primitives.DataGridColumnHeader;
                    if (null != dgch)
                    {
                        columnName = dgch.Column.SortMemberPath.ToString();
                    }
                    //
                    switch (columnName)
                    {
                        case "tagArtist":
                            SetTagArtist(Utils.CleanDrop(d));
                            break;
                        case "tagAlbum":
                            SetTagAlbum(Utils.CleanDrop(d));
                            break;
                        case "tagYear":
                            SetTagYear(verifyNumber(Utils.CleanDrop(d), 4));
                            break;
                        case "tagGenre":
                            SetTagGenre(Utils.CleanDrop(d));
                            break;
                        default:
                            e.Effects = DragDropEffects.None;
                            break;
                    }
                    //
                    return;
                }
                #endregion

                #region CellDrop
                if (dep is DataGridCell)
                {
                    DataGridCell cell = dep as DataGridCell;
                    //http://www.mycsharp.de/wbb2/thread.php?threadid=81297
                    HitTestResult result = VisualTreeHelper.HitTest(this, e.GetPosition(this));
                    if ((result != null) && (result.VisualHit.GetType() == typeof(TextBlock)))
                    {
                        TextBlock tblock = (TextBlock)(result.VisualHit);
                        switch (cell.Column.SortMemberPath)
                        {
                            case "FileName":
                                tblock.Text = Utils.CleanFileName(d);
                                //((CData)tblock.DataContext).FileName_Changed = true;
                                //((CData)tblock.DataContext).Marker = true;
                                break;
                            case "FileTrack":
                                tblock.Text = verifyNumber(Utils.CleanDrop(d), 3);
                                break;
                            case "tagTrack":
                                tblock.Text = verifyNumber(Utils.CleanDrop(d), 3);
                                //
                                break;
                            case "tagTitle":
                                tblock.Text = Utils.CleanDrop(d);
                                //
                                break;
                            case "tagArtist":
                                tblock.Text = Utils.CleanDrop(d);
                                //
                                break;
                            case "tagAlbum":
                                tblock.Text = Utils.CleanDrop(d);
                                //
                                break;
                            case "tagYear":
                                tblock.Text = verifyNumber(Utils.CleanDrop(d), 4);
                                //
                                break;
                            case "tagGenre":
                                tblock.Text = Utils.CleanDrop(d);
                                //
                                break;
                        }
                    }
                }
                #endregion
            }
            //---------------------------------------------------------------------------------------------------------------------------------
            else if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                #region FileDrop
                e.Effects = DragDropEffects.Copy;
                    //
                    string targetDir = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
                    //
                    if (".CUE" == Path.GetExtension(targetDir).ToUpper())
                    {
                        ReadCueFile rcf = new ReadCueFile();
                        if (rcf.ReadCue(targetDir))
                        {
                            if (0 != rcf.TRACKS.Length)
                            {
                                for (int i = 0; i < rcf.TRACKS.Length; i++)
                                {
                                    SetTagTitleAuthor(i, rcf.TRACKS[i].TITLE, rcf.TRACKS[i].PERFORMER);
                                }
                                if ("" != rcf.TITLE)
                                {
                                    SetTagAlbum(rcf.TITLE);
                                }
                                if ("" != rcf.PERFORMER)
                                {
                                    SetTagArtist(rcf.PERFORMER);
                                }
                                if ("" != rcf.DATE)
                                {
                                    SetTagYear(rcf.DATE);
                                }
                            }
                        }
                    }
                    else
                    {
                        MusicFilesExtensions = DefaultMusicFilesExtensions;
                        //
                        if (!Directory.Exists(targetDir))//zapewne to plik
                        {
                            MusicFilesExtensions = new string[] { Path.GetExtension(targetDir) };
                            targetDir = Path.GetDirectoryName(targetDir);
                        }
                        if (Directory.Exists(targetDir))
                        {
                            StartDirectory = targetDir;
                            WaitingTooLong wtl = new WaitingTooLong(5);
                            loadDir();
                            dataGrid1.Items.Refresh();
                            wtl.isTooLongBeep(new WindowInteropHelper(this).Handle);
                            afterLoad(true);
                            setInfoFromPath(StartDirectory);
                        }
                    }
                //
                #endregion
            }
            //---------------------------------------------------------------------------------------------------------------------------------
            if (e.Data.GetDataPresent(typeof(HeaderDropInfo)))
            {
                #region Header2HeaderDrop
                string srcCol = ((string)((HeaderDropInfo)e.Data.GetData(typeof(HeaderDropInfo))).Name);

                DependencyObject dep = (DependencyObject)e.OriginalSource;

                // iteratively traverse the visual tree
                while ((dep != null) && !(dep is DataGridCell) && !(dep is System.Windows.Controls.Primitives.DataGridColumnHeader))
                {
                    if (!(dep is DependencyObject)) { dep = null; break; }
                    if (dep is System.Windows.Documents.Run) { dep = null; break; } 
                    dep = VisualTreeHelper.GetParent(dep);
                }

                if (dep == null) return;
                System.Windows.Controls.Primitives.DataGridColumnHeader cell = dep as System.Windows.Controls.Primitives.DataGridColumnHeader;
                string dstCol = cell.Column.SortMemberPath;
                if ("tagTitle" == srcCol && "FileName" == dstCol)
                {
                    e.Effects = DragDropEffects.Copy;
                    Copy_tagTitle_2_FileName();
                }
                else if ("FileName" == srcCol && "tagTitle" == dstCol)
                {
                    e.Effects = DragDropEffects.Copy;
                    Copy_FileName_2_tagTitle();
                }
                else if ("tagTrack" == srcCol && "FileTrack" == dstCol)
                {
                    e.Effects = DragDropEffects.Copy;
                    Copy_tagTrack_2_FileTrack();
                }
                else if ("FileTrack" == srcCol && "tagTrack" == dstCol)
                {
                    e.Effects = DragDropEffects.Copy;
                    Copy_FileTrack_2_tagTrack();
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                }

                //
                return;
                #endregion
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        //---------------------------------------------------------------------------------------------------------------------------------
        //
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        private void buttonSetTagArtist_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagArtist_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagArtist_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                //artist = Utils.swapThe(artist);// chyba nie
                SetTagArtist(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)));
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        private void buttonSetTagAlbum_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagAlbum_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagAlbum_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                SetTagAlbum(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)));
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        private void buttonSetTagYear_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagYear_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagYear_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                SetTagYear(verifyNumber(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)),4));
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        private void buttonSetTagGenre_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagGenre_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void buttonSetTagGenre_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                SetTagGenre(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)));
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------
    }//CLASS
}//namesapce
